package com.qtummatrix.dao.impl;

import com.qtummatrix.dao.CustomerDao;
import com.qtummatrix.entity.CustomerInfoVO;
import com.qtummatrix.entity.CustomerinfoEntity;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * create by Gongshiyong  2019-11-21 19:39
 */

@Repository
public class CustomerDaoImpl implements CustomerDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	private Session getSession() {
		return sessionFactory.getCurrentSession();
		
	}

	@Override
	public void delCustomer(int id) {
		String sql="delete from CustomerinfoEntity where id=?";
		getSession().createQuery(sql).setInteger(0, id).executeUpdate();
	}

	@Override
	public void addCustomer(String name, String gender, String tel, String UID, String address, String customerWork,
			String customerType, String discredit, String litigation, int monthlyIncome, int bankId, int goodsId,String customerState) {
		CustomerinfoEntity customerinfoEntity=new CustomerinfoEntity();
		customerinfoEntity.setCustomername(name);
		customerinfoEntity.setGender(gender);
		customerinfoEntity.setTel(tel);
		customerinfoEntity.setUid(UID);
		customerinfoEntity.setAddress(address);
		customerinfoEntity.setCustomerwork(customerWork);
		customerinfoEntity.setCustomertype(customerType);
		customerinfoEntity.setDiscredit(discredit);
		customerinfoEntity.setLitigation(litigation);
		customerinfoEntity.setMonthlyincome(monthlyIncome);
		customerinfoEntity.setBankid(bankId);
		customerinfoEntity.setGoodsid(goodsId);
		customerinfoEntity.setCustomerstate(customerState);
		Long time=System.currentTimeMillis();
		Date date=new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime=dateFormat.format(date);
		customerinfoEntity.setCreatetime(Timestamp.valueOf(createTime));
		getSession().save(customerinfoEntity);
		
	}

	@Override
	public List<CustomerInfoVO> findCustomer(int id, String name, String customerType, String UID , int first, int rows) {
		StringBuilder sql=new StringBuilder("select c.id,customerName,customerType,UID,bankName,customerState,createTime,pledgeName from CustomerinfoEntity as c "
				+ "inner join PledgeEntity as p on c.goodsId=p.id "
				+ "inner join BankinfoEntity as b on c.bankId=b.id "
				+ " where 1=1 ");
		boolean bid=id==0;
		boolean bname=name==null||name.equals("");
		boolean bcustomerType=customerType==null||customerType.equals("");
		boolean bUID=UID==null||UID.equals("");
		if(!bid) {
			sql.append(" and c.id=?");
		}
		if(!bname) {
			sql.append(" and name=?");
		}
		if(!bcustomerType) {
			sql.append(" and customerType=?");
		}
		if(!bUID) {
			sql.append(" and UID=?");
		}
	
		SQLQuery query=getSession().createSQLQuery(sql.toString());
		int index=0;
		if(!bid) {
			query.setInteger(index, id);
			index++;
		}
		
		if(!bname) {
			query.setString(index,name);
			index++;
		}
		if(!bcustomerType) {
			query.setString(index,customerType);
			index++;
		}
		if(!bUID) {
			query.setString(index,UID );
			index++;
		}
		/*query.setInteger(index,first);
		index++;
		query.setInteger(index,rows);*/
		List<CustomerInfoVO> customerInfoVOs=new ArrayList<CustomerInfoVO>();
		List<Object[]> list=query.setFirstResult(first).setMaxResults(rows).list();
		for(Object[] object:list) {
			CustomerInfoVO customerInfoVO=new CustomerInfoVO();
			customerInfoVO.setId((int)object[0]);
			customerInfoVO.setCustomerName(object[1].toString());
			customerInfoVO.setCustomerType(object[2].toString());
			customerInfoVO.setUID(object[3].toString());
			customerInfoVO.setBankName(object[4].toString());
			customerInfoVO.setCustomerState(object[5].toString());
			if(object[6]!=null) {
 			    customerInfoVO.setCreateTime(object[6].toString());
			}
			customerInfoVO.setPledgeName(object[7].toString());
			customerInfoVOs.add(customerInfoVO);
		}
		
		return customerInfoVOs; 
		
	}

	@Override
	public List<CustomerinfoEntity> findCustomer() {
		String sql ="from CustomerinfoEntity ";
		List<CustomerinfoEntity> list=getSession().createQuery(sql).list();
		return list;
	}

}
